/**
 * Title:        RAC和sequence缓存缓存检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

import org.json.JSONObject;

import weaver.email.EmailEncoder;
import weaver.email.Email_Autherticator;
import weaver.general.GCONST;
import weaver.general.OrderProperties;
import weaver.general.Util;
import weaver.system.SystemComInfo;


public class RacAndSeqCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        List resultList = new ArrayList();
        String result = "";
        resultNeedFix = resultOk;
        if (recordSet.getDBType().equals("oracle")) {
            recordSet.executeSql("select * from v$option WHERE Parameter = 'Real Application Clusters'");
            if (recordSet.next()) {
                String val = Util.null2String(recordSet.getString("value"));
                if (val.toLowerCase().equals("true")) {
                    
                    String[] seqs = new String[]{"WORKFLOW_NODELINK_ID", "WORKFLOW_NODEBASE_ID"};
                    for (int i=0; i<seqs.length; i++) {
                        recordSet.executeSql("select cache_size from user_sequences where sequence_name = '" + seqs[i] + "' ");
                        if (recordSet.next()) {
                            int int_val = Util.getIntValue(recordSet.getString("cache_size"));
                            if (int_val > 0) {
                                result = resultNeedFix;
                                Map resultMap = new HashMap<String,String>();
                                resultMap.put("showMessage","开启了rac， 且设置sequence缓存！！");
                                resultMap.put("buttonname","关闭");
                                resultList.add(resultMap);
                                return formatReturnJsonCheck(result, resultList,this);
                            }
                        }
                    }
                }
            }
        }
        (new BaseBean()).writeLog("CheckOvertime  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        
        if(dataMap!= null && dataMap.get("divid")!= null){
            List resultList = new ArrayList();
            String divid = (String)dataMap.get("divid");
            String result = "";
            resultNeedFix = resultOk;
            if (recordSet.getDBType().equals("oracle")) {
                recordSet.executeSql("select * from v$option WHERE Parameter = 'Real Application Clusters'");
                if (recordSet.next()) {
                    String val = Util.null2String(recordSet.getString("value"));
                    if (val.toLowerCase().equals("true")) {

                        String[] seqs = new String[]{"WORKFLOW_NODELINK_ID", "WORKFLOW_NODEBASE_ID"};
                        for (int i=0; i<seqs.length; i++) {
                            recordSet.executeUpdate("alter sequence " + seqs[i] + " nocache");
                        }
                        String[] seqs1 = new String[]{"WORKFLOW_NODELINK_ID", "WORKFLOW_NODEBASE_ID"};
                        for (int i=0; i<seqs.length; i++) {
                            recordSet.executeSql("select cache_size from user_sequences where sequence_name = '" + seqs[i] + "' ");
                            if (recordSet.next()) {
                                int int_val = Util.getIntValue(recordSet.getString("cache_size"));
                                if (int_val > 0) {
                                    result = resultNeedFix;
                                    Map resultMap = new HashMap<String,String>();
                                    resultMap.put("showMessage","开启了rac， 且设置sequence缓存！！");
                                    resultMap.put("buttonname","关闭");
                                    resultMap.put("divid",divid);
                                    resultList.add(resultMap);
                                    return formatReturnJsonCheck(result, resultList,this);
                                }
                            }
                        }
                    }
                }
            }
            return formatReturnJsonFix(result, resultList,this);
        }else{
            List resultList = new ArrayList();
            String divid = (String)dataMap.get("divid");
            String result = "";
            resultNeedFix = resultOk;
            if (recordSet.getDBType().equals("oracle")) {
                recordSet.executeSql("select * from v$option WHERE Parameter = 'Real Application Clusters'");
                if (recordSet.next()) {
                    String val = Util.null2String(recordSet.getString("value"));
                    if (val.toLowerCase().equals("true")) {

                        String[] seqs = new String[]{"WORKFLOW_NODELINK_ID", "WORKFLOW_NODEBASE_ID"};
                        for (int i=0; i<seqs.length; i++) {
                            recordSet.executeUpdate("alter sequence " + seqs[i] + " nocache");
                        }
                    }
                }
            }
            return checkData(dataMap);
        }
    }

    @Override
    public String getClassName() {
        return "RAC和sequence缓存缓存检测";
    }

    @Override
    public String getClassDesc() {
        // TODO Auto-generated method stub
        return "解决流程图/节点编辑 保存后 节点丢失、产生多个同名节点的问题";
    }

}



